package com.cd.util;

import java.sql.*;

public class DBUtil {

    // 1. 定义一串数据库连接信息
    private static final String DRV = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/movie_db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai";
    private static final String USER = "root";
    private static final String PWD = "1234";

    // 2. 利用反射加载驱动
    static {
        try {
            Class.forName(DRV);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 3. 获取数据库连接
    public static Connection getConn() {
        try {
            return DriverManager.getConnection(URL, USER, PWD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 4. 关闭数据库连接
    public static void close(Connection conn, ResultSet rs, Statement ps) {
        try {
            if (ps != null) ps.close();
            if (rs != null) rs.close();
            if (conn != null) conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
